package cn.flying.cloud.oauth.server.configuration.handler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson2.JSON;

import cn.flying.cloud.base.common.enums.RtCodeEnum;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.oauth.server.configuration.exception.CaptchaValidException;
import cn.flying.cloud.oauth.server.configuration.exception.TokenExpiredException;
import cn.flying.cloud.oauth.server.configuration.exception.TokenIllegalException;

/**
 * @description: 登录认证失败处理类
 * @author: admin
 * @date: 2023年05月25日 12:54
 * @version: 1.0
 */
@Component
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * Called when an authentication attempt fails.
     *
     * @param request   the request during which the authentication attempt occurred.
     * @param response  the response.
     * @param exception the exception which was thrown to reject the authentication
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        logger.error("登录失败！", exception);
        Rt<?> rt;
        //判断异常类型
        if (exception instanceof AccountExpiredException) {
            //账户过期
            rt = Rt.error(RtCodeEnum.R10011);
        } else if (exception instanceof BadCredentialsException) {
            //用户名或者密码输入错误
            rt = Rt.error(RtCodeEnum.R10003);
        } else if (exception instanceof CredentialsExpiredException) {
            //密码过期
            rt = Rt.error(RtCodeEnum.R10010);
        } else if (exception instanceof DisabledException) {
            //账户被禁用
            rt = Rt.error(RtCodeEnum.R10012);
        } else if (exception instanceof LockedException) {
            //账户被锁定
            rt = Rt.error(RtCodeEnum.R10002);
        } else if (exception instanceof InternalAuthenticationServiceException) {
            //账号不存在
            rt = Rt.error(RtCodeEnum.R10001);
        } else if (exception instanceof CaptchaValidException) {
            //验证码错误
            rt = Rt.error(RtCodeEnum.R10004);
        } else if (exception instanceof TokenExpiredException) {
            // token失效
            rt = Rt.error(RtCodeEnum.R10005);
        } else if (exception instanceof TokenIllegalException) {
            rt = Rt.error(RtCodeEnum.R10005);
        } else {
            rt = Rt.error(RtCodeEnum.R10013);
        }

        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        //登录成功返回用户信息
        out.write(JSON.toJSONString(rt));
        out.flush();
        out.close();
    }
}
